package fitnesshub.controller;

import fitnesshub.pojo.dto.AddEquipmentDTO;
import fitnesshub.pojo.dto.AdminInfoDTO;
import fitnesshub.pojo.dto.AdminLoginDTO;
import fitnesshub.pojo.entity.Admin;
import fitnesshub.pojo.entity.Coach;
import fitnesshub.pojo.vo.AdminLoginVO;
import fitnesshub.properties.JwtProperties;
import fitnesshub.result.Result;
import fitnesshub.service.AdminService;
import fitnesshub.utils.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 管理员-管理
 */
@RestController
@RequestMapping("/admin")
@Slf4j
public class AdminController {
    @Autowired
    private AdminService adminService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 管理员登录
     * @param adminLoginDTO 管理员登录时传递过来的数据
     * @return
     */
    @PostMapping("/login")
    public Result<AdminLoginVO> login(@RequestBody AdminLoginDTO adminLoginDTO) {
        log.info("管理员登录：{}", adminLoginDTO);

        Admin admin = adminService.login(adminLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put("adminId", admin.getAdminId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        AdminLoginVO adminLoginVO = new AdminLoginVO(
                admin.getAdminId(),
                admin.getUsername(),
                admin.getAdminName(),
                admin.getAvatar(),
                token
        );

        return Result.success(adminLoginVO);
    }

    /**
     * 管理员登出
     * @return 登出结果(ok)
     */
    @PostMapping("/logout")
    public Result logout() {
        log.info("管理员退出登录");
        return Result.success();
    }



    /**
     * 查看管理员
     * @return
     */
    @GetMapping("/info")
    public Result<List<Admin>> getAdminInfo() {
        log.info("管理员的信息");
        List<Admin> adminList = adminService.getAdminInfo();
        return Result.success(adminList);
    }

}


